home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / ASM-SPAR.{_A / ATOMIC.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  2KB  |  94 lines

  1. /* $Id: atomic.h,v 1.18 1997/08/07 03:38:31 davem Exp $
  2.  * atomic.h: Thankfully the V9 is at least reasonable for this
  3.  *           stuff.
  4.  *
  5.  * Copyright (C) 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
  6.  */
  7.  
  8. #ifndef __ARCH_SPARC64_ATOMIC__
  9. #define __ARCH_SPARC64_ATOMIC__
  10.  
  11. /* Make sure gcc doesn't try to be clever and move things around
  12.  * on us. We need to use _exactly_ the address the user gave us,
  13.  * not some alias that contains the same information.
  14.  */
  15. #define __atomic_fool_gcc(x) ((struct { int a[100]; } *)x)
  16.  
  17. typedef struct { int counter; } atomic_t;
  18. #define ATOMIC_INIT(i)    { (i) }
  19.  
  20. #define atomic_read(v)        ((v)->counter)
  21. #define atomic_set(v, i)    (((v)->counter) = i)
  22.  
  23. extern __inline__ void atomic_add(int i, atomic_t *v)
  24. {
  25.     __asm__ __volatile__("
  26. 1:    lduw        [%1], %%g5
  27.     add        %%g5, %0, %%g7
  28.     cas        [%1], %%g5, %%g7
  29.     sub        %%g5, %%g7, %%g5
  30.     brnz,pn        %%g5, 1b
  31.      nop"
  32.     : /* No outputs */
  33.     : "HIr" (i), "r" (__atomic_fool_gcc(v))
  34.     : "g5", "g7", "memory");
  35. }
  36.  
  37. extern __inline__ void atomic_sub(int i, atomic_t *v)
  38. {
  39.     __asm__ __volatile__("
  40. 1:    lduw        [%1], %%g5
  41.     sub        %%g5, %0, %%g7
  42.     cas        [%1], %%g5, %%g7
  43.     sub        %%g5, %%g7, %%g5
  44.     brnz,pn        %%g5, 1b
  45.      nop"
  46.     : /* No outputs */
  47.     : "HIr" (i), "r" (__atomic_fool_gcc(v))
  48.     : "g5", "g7", "memory");
  49. }
  50.  
  51. /* Same as above, but return the result value. */
  52. extern __inline__ int atomic_add_return(int i, atomic_t *v)
  53. {
  54.     unsigned long oldval;
  55.     __asm__ __volatile__("
  56. 1:    lduw        [%2], %%g5
  57.     add        %%g5, %1, %%g7
  58.     cas        [%2], %%g5, %%g7
  59.     sub        %%g5, %%g7, %%g5
  60.     brnz,pn        %%g5, 1b
  61.      add        %%g7, %1, %0"
  62.     : "=&r" (oldval)
  63.     : "HIr" (i), "r" (__atomic_fool_gcc(v))
  64.     : "g5", "g7", "memory");
  65.     return (int)oldval;
  66. }
  67.  
  68. extern __inline__ int atomic_sub_return(int i, atomic_t *v)
  69. {
  70.     unsigned long oldval;
  71.     __asm__ __volatile__("
  72. 1:    lduw        [%2], %%g5
  73.     sub        %%g5, %1, %%g7
  74.     cas        [%2], %%g5, %%g7
  75.     sub        %%g5, %%g7, %%g5
  76.     brnz,pn        %%g5, 1b
  77.      sub        %%g7, %1, %0"
  78.     : "=&r" (oldval)
  79.     : "HIr" (i), "r" (__atomic_fool_gcc(v))
  80.     : "g5", "g7", "memory");
  81.     return (int)oldval;
  82. }
  83.  
  84. #define atomic_dec_return(v) atomic_sub_return(1,(v))
  85. #define atomic_inc_return(v) atomic_add_return(1,(v))
  86.  
  87. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  88. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  89.  
  90. #define atomic_inc(v) atomic_add(1,(v))
  91. #define atomic_dec(v) atomic_sub(1,(v))
  92.  
  93. #endif /* !(__ARCH_SPARC64_ATOMIC__) */
  94.